home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 11 object serialization / serializationdemo / module1.vb < prev    next >
Encoding:
Text File  |  2002-03-20  |  10.5 KB  |  315 lines

  1. Imports System.IO
  2. Imports System.Runtime.Serialization
  3. Imports System.Runtime.Serialization.Formatters.Binary
  4. Imports System.Runtime.Serialization.Formatters.Soap
  5.  
  6. Module MainModule
  7.  
  8.     Sub Main()
  9.         ' Run one of the Textxxxx procedures below by uncommenting only one statement
  10.  
  11.         'TestSaveArray()
  12.         'TestLoadArray()
  13.         'TestSoapSerialization()
  14.         'TestSerializableClass()
  15.         'TestSerializableGraph()
  16.         'TestCloneObject()
  17.         'TestCustomSerialization()
  18.         'TestCustomSerialization2()
  19.         'TestIDeserializationCallback()
  20.  
  21.         ' These statements are usuful when running inside Visual Studio.NET
  22.         Console.WriteLine("")
  23.         Console.WriteLine(">>> Press Enter to terminate the program <<<")
  24.         Console.ReadLine()
  25.     End Sub
  26.  
  27.     ' this procedure tests the serialization of an array of integer
  28.  
  29.     Sub TestSaveArray()
  30.         ' Create an array of integers.
  31.         Dim arr() As Integer = {1, 2, 4, 8, 16, 32, 64, 128, 256}
  32.  
  33.         ' Open a file stream for output.
  34.         Dim fs As FileStream = New FileStream("c:\powers.dat", FileMode.Create)
  35.         ' Create a binary formatter for this stream.
  36.         Dim bf As New BinaryFormatter()
  37.  
  38.         ' Serialize the array to the file stream, and flush the stream.
  39.         bf.Serialize(fs, arr)
  40.         fs.Close()
  41.     End Sub
  42.  
  43.     ' this procedure deserializes an array of integers
  44.  
  45.     Sub TestLoadArray()
  46.         ' Open a file stream for input.
  47.         Dim fs As FileStream = New FileStream("c:\powers.dat", FileMode.Open)
  48.         ' Create a binary formatter for this stream.
  49.         Dim bf As New BinaryFormatter()
  50.  
  51.         ' Deserialize the contents of the file stream into an Integer array.
  52.         Dim arr() As Integer
  53.         ' Deserialize returns an object that must be coerced.
  54.         arr = DirectCast(bf.Deserialize(fs), Integer())
  55.  
  56.         ' Display the result.
  57.         Dim n As Integer
  58.         For Each n In arr
  59.             Console.Write(n.ToString & " ")
  60.         Next
  61.         Console.WriteLine("")
  62.     End Sub
  63.  
  64.     ' these reusable procedure save and load data in SOAP format
  65.  
  66.     ' Serialize an object to file in SOAP format.
  67.     Sub SaveSoapData(ByVal path As String, ByVal o As Object)
  68.         ' Open a file stream for output.
  69.         Dim fs As FileStream = New FileStream(path, FileMode.Create)
  70.         ' Create a SOAP formatter for this stream.
  71.         Dim sf As New SoapFormatter(Nothing, New StreamingContext(StreamingContextStates.File))
  72.         ' Serialize the array to the file stream, and close the stream.
  73.         sf.Serialize(fs, o)
  74.         fs.Close()
  75.     End Sub
  76.  
  77.     ' Deserialize an object from a file in SOAP format.
  78.     Function LoadSoapData(ByVal path As String) As Object
  79.         ' Open a file stream for input.
  80.         Dim fs As FileStream = New FileStream(path, FileMode.Open)
  81.         ' Create a SOAP formatter for this stream.
  82.         Dim sf As New SoapFormatter(Nothing, New StreamingContext(StreamingContextStates.File))
  83.  
  84.         ' Deserialize the contents of the file stream into an object.
  85.         LoadSoapData = sf.Deserialize(fs)
  86.         ' close the stream.
  87.         fs.Close()
  88.     End Function
  89.  
  90.     ' this procedure tests the reusable routines above
  91.  
  92.     ' An example that uses the above routines.
  93.     Sub TestSoapSerialization()
  94.         ' Create a hashtable object and fill it with some data.
  95.         Dim ht As New Hashtable()
  96.         ht.Add("One", 1)
  97.         ht.Add("Two", 2)
  98.         ht.Add("Three", 3)
  99.  
  100.         ' Save the hash table to disk in SOAP format.
  101.         SaveSoapData("c:\hashtbl.xml", ht)
  102.  
  103.         ' Reload the file contents into another HashTable object.
  104.         Dim ht2 As Hashtable
  105.         ht2 = CType(LoadSoapData("c:\hashtbl.xml"), Hashtable)
  106.  
  107.         ' Display values.
  108.         Dim de As DictionaryEntry
  109.         For Each de In ht2
  110.             Console.WriteLine("Key=" & de.Key.ToString & _
  111.                 "   Value=" & de.Value.ToString)
  112.         Next
  113.     End Sub
  114.  
  115.     ' this procedure tests the <serializable> and <notserialized> attributes
  116.  
  117.     Sub TestSerializableClass()
  118.         Dim al As New ArrayList()
  119.         al.Add(New Person("Joe", "Doe", #1/12/1960#))
  120.         al.Add(New Person("John", "Smith", #3/6/1962#))
  121.         al.Add(New Person("Ann", "Doe", #10/4/1965#))
  122.  
  123.         ' Save the ArrayList to disk in SOAP format.
  124.         SaveSoapData("c:\hashtbl.xml", al)
  125.  
  126.         ' Reload the file contents into another ArrayList object.
  127.         Dim al2 As ArrayList
  128.         al2 = DirectCast(LoadSoapData("c:\hashtbl.xml"), ArrayList)
  129.  
  130.         ' Display values.
  131.         Dim p As Person
  132.         For Each p In al2
  133.             Console.WriteLine(p.FirstName & " " & p.LastName & " (" & p.Age & ")")
  134.         Next
  135.     End Sub
  136.  
  137.     ' this procedure tests the serialization of an object graph
  138.  
  139.     Sub TestSerializableGraph()
  140.         ' Create three Person objects.
  141.         Dim p1 As New Person("Joe", "Doe", #1/12/1960#)
  142.         Dim p2 As New Person("John", "Smith", #3/6/1962#)
  143.         Dim p3 As New Person("Ann", "Doe", #10/4/1965#)
  144.         ' Define the relationship between two of them.
  145.         p2.Spouse = p3
  146.         p3.Spouse = p2
  147.  
  148.         ' Load them into an ArrayList object.
  149.         Dim al As New ArrayList()
  150.         al.Add(p1)
  151.         al.Add(p2)
  152.         al.Add(p3)
  153.  
  154.         ' Save the hash table to disk in XML format
  155.         SaveSoapData("c:\hashtbl.xml", al)
  156.  
  157.         ' Reload the file contents into another HashTable object.
  158.         Dim al2 As ArrayList
  159.         al2 = DirectCast(LoadSoapData("c:\hashtbl.xml"), ArrayList)
  160.  
  161.         ' Display values.
  162.         Dim p As Person
  163.         For Each p In al2
  164.             Console.WriteLine(p.FirstName & " " & p.LastName & " (" & p.Age & ")")
  165.             If Not (p.Spouse Is Nothing) Then
  166.                 ' Show the spouse's name, if there is one.
  167.                 Console.WriteLine("   Spouse of " & p.Spouse.FirstName)
  168.             End If
  169.         Next
  170.     End Sub
  171.  
  172.     ' this procedure tests object cloning
  173.  
  174.     Sub TestCloneObject()
  175.         ' create an object graph
  176.         Dim p1 As New Person("Joe", "Doe", #1/2/1960#)
  177.         Dim p2 As New Person("Ann", "Smith", #3/4/1965#)
  178.         p1.Spouse = p2
  179.         p2.Spouse = p1
  180.  
  181.         ' Clone it
  182.         Dim q1 As Person = DirectCast(CloneObject(p1), Person)
  183.         Dim q2 As Person = q1.Spouse
  184.         ' Prove that properties were copied correctly.
  185.         Console.WriteLine(q1.FirstName & " " & q1.LastName)
  186.         Console.WriteLine(q2.FirstName & " " & q2.LastName)
  187.         Console.WriteLine("P1 is Q1 = {0}", p1 Is q1)
  188.         Console.WriteLine("P2 is Q2 = {0}", p2 Is q2)
  189.     End Sub
  190.  
  191.     ' a reusable function that does object cloning
  192.  
  193.     Function CloneObject(ByVal obj As Object) As Object
  194.         ' Create a memory stream and a formatter.
  195.         Dim ms As New System.IO.MemoryStream(1000)
  196.         Dim bf As New BinaryFormatter(Nothing, New StreamingContext(StreamingContextStates.Clone))
  197.         ' Serialize the object into the stream.
  198.         bf.Serialize(ms, obj)
  199.         ' Position streem pointer back to first byte.
  200.         ms.Seek(0, SeekOrigin.Begin)
  201.         ' Deserialize into another object.
  202.         CloneObject = bf.Deserialize(ms)
  203.         ' release memory.
  204.         ms.Close()
  205.     End Function
  206.  
  207.     ' this procedure tests the ISerializable interface
  208.  
  209.     Sub TestCustomSerialization()
  210.         ' Create a compact array of Double, whose DefaultValue is 1.
  211.         Dim ca As New CompactDoubleArray()
  212.         ca.DefaultValue = 1
  213.         ' Add some elements (including some default values).
  214.         ca.Add(1)
  215.         ca.Add(2)
  216.         ca.Add(3)
  217.         ca.Add(1)
  218.         ca.Add(4)
  219.         ca.Add(5)
  220.         ca.Add(1)
  221.         ' Serialize the array.
  222.         SaveSoapData("c:\compact.xml", ca)
  223.  
  224.         ' Read it back.
  225.         Dim ca2 As CompactDoubleArray
  226.         ca2 = DirectCast(LoadSoapData("c:\compact.xml"), CompactDoubleArray)
  227.  
  228.         ' Print its contents.
  229.         Dim o As Object
  230.         For Each o In ca2
  231.             Console.Write(o.ToString & " ")
  232.         Next
  233.         Console.WriteLine("")
  234.     End Sub
  235.  
  236.     ' this procedure is identical to previous one, but it uses CompactDoubleArray2
  237.  
  238.     Sub TestCustomSerialization2()
  239.         ' Create a compact array of Double, whose DefaultValue is 1.
  240.         Dim ca As New CompactDoubleArray2()
  241.         ca.DefaultValue = 1
  242.         ' Add some elements (including some default values).
  243.         ca.Add(1)
  244.         ca.Add(2)
  245.         ca.Add(3)
  246.         ca.Add(1)
  247.         ca.Add(4)
  248.         ca.Add(5)
  249.         ca.Add(1)
  250.  
  251.         ' Serialize the array to a memory stream (to prove it isn't compat
  252.         SaveSoapData("c:\compact.xml", ca)
  253.  
  254.         ' Read it back.
  255.         Dim ca2 As CompactDoubleArray2
  256.         ca2 = DirectCast(LoadSoapData("c:\compact.xml"), CompactDoubleArray2)
  257.  
  258.         ' Print its contents.
  259.         Dim o As Object
  260.         For Each o In ca2
  261.             Console.Write(o.ToString & " ")
  262.         Next
  263.         Console.WriteLine("")
  264.  
  265.         ' Serialize the array to a memory stream (to prove it isn't compacted)
  266.  
  267.         ' Create the memory stream
  268.         Dim ms As New MemoryStream(10000)
  269.         ' Create a binary formatter for this stream
  270.         Dim bf As New BinaryFormatter()
  271.         ' serialize the CompactArrayDouble object to the stream
  272.         bf.Serialize(ms, ca)
  273.  
  274.         ' rewind the memory stream and read its contents back into CA2
  275.         ms.Seek(0, SeekOrigin.Begin)
  276.         ca2 = DirectCast(bf.Deserialize(ms), CompactDoubleArray2)
  277.         ' close the stream
  278.         ms.Close()
  279.  
  280.         For Each o In ca2
  281.             Console.Write(o.ToString & " ")
  282.         Next
  283.         Console.WriteLine("")
  284.     End Sub
  285.  
  286.     ' this procedure tests IDeserializationCallback interface
  287.  
  288.     Sub TestIDeserializationCallback()
  289.         ' Create a compact array of Point objects.
  290.         Dim ca As New CompactPointArray()
  291.         ' Add some elements (including some (0,0) points).
  292.         ca.Add(New Point(1, 3))
  293.         ca.Add(New Point(0, 0))
  294.         ca.Add(New Point(3, 5))
  295.         ca.Add(New Point(1, 6))
  296.         ca.Add(New Point(0, 0))
  297.         ca.Add(New Point(4, 8))
  298.         ca.Add(New Point(0, 0))
  299.         ' Serialize it.
  300.         SaveSoapData("c:\points.xml", ca)
  301.  
  302.         ' Read it back.
  303.         Dim ca2 As CompactPointArray
  304.         ca2 = DirectCast(LoadSoapData("c:\Points.xml"), CompactPointArray)
  305.  
  306.         ' Print its contents.
  307.         Dim o As Object
  308.         For Each o In ca2
  309.             Console.Write(o.ToString & " ")
  310.         Next
  311.         Console.WriteLine("")
  312.     End Sub
  313.  
  314. End Module
  315.